clear all
global filepath "C:\Users\karti\Documents\NSS_Labor_Market\Extracted_NSS"
set more off
cd "$filepath"



use "C:\Users\karti\Documents\Data\regressions\paper_2\data_corrected_for_regs_DDD_with nssdist.dta", clear

tab yr_qtr, gen(yr_qtr_id)

*tab year, gen(yr_id)

tab new_state_dist_code, gen(nssdist)
gen t2 =  time - 1
forvalues j = 1/150 {
*linear
gen nssdist_`j'=nssdist`j'*t2
}


foreach var of varlist wheat_prod rice_prod no_agri_worker_united agri_worker cultivator cult_daily cult_days ag_days{
gen `var'_post = `var'*post_dif 
}


**DD NO RESULTS

global indiv_controls "age hhsize hindu muslim r_others sc st c_others"
global dist_controls "wheat_prod rice_prod no_agri_worker_united agri_worker"

areg log_real_agwage interact1 marketing_reform post_dif i.yr_qtr  [aw=weight], cluster(new_state_dist_code) absorb(new_state_dist_code)
outreg2 using r_indivdd.xls, replace  ctitle("") keep(interact1) addtext(District FE, YES, Year Quarter FE, YES, Controls, NO, District Specific Linear Time Trend, NO) nocons


areg log_real_agwage interact1 marketing_reform post_dif i.yr_qtr $indiv_controls $dist_controls [aw=weight], cluster(new_state_dist_code) absorb(new_state_dist_code)
outreg2 using r_indivdd.xls, append  ctitle("") keep(interact1) addtext(District FE, YES, Year Quarter FE, YES, Controls, YES, District Specific Linear Time Trend, YES) nocons

areg log_real_agwage interact1 marketing_reform post_dif i.yr_qtr $indiv_controls $dist_controls nssdist_* [aw=weight], cluster(new_state_dist_code) absorb(new_state_dist_code)
outreg2 using r_indivdd.xls, append  ctitle("") keep(interact1) addtext(District FE, YES, Year Quarter FE, YES, Controls, YES, District Specific Linear Time Trend, YES) nocons


egen dt = group(new_state_dist_code)
reg log_real_agwage interact1 marketing_reform post_dif i.yr_qtr i.dt $indiv_controls $dist_controls nssdist_*, vce(boot,cluster(dt) idcluster(nnewid) reps(400) seed(10101)) 


*****************************************************************************************************************

** Robustness

** chand 
use "C:\Users\karti\Documents\Data\regressions\paper_2\data_corrected_for_regs_DDD_with nssdist.dta", clear

keep if year<=2005

tab new_state_dist_code, gen(nssdist)
gen t2 =  time - 1
forvalues j = 1/150 {
*linear
gen nssdist_`j'=nssdist`j'*t2
}


gen t_c = tr*chand_reform 


areg log_real_agwage t_c chand_reform i.yr_qtr $indiv_controls  $dist_controls nssdist_* [aweight=weight], cluster(new_state_dist_code) absorb(new_state_dist_code)
outreg2 using chand_robust.xls, replace  ctitle("") keep(t_c) addtext(District FE, YES, Year Quarter FE, YES, Controls, YES, District Level Linear Time Trend, YES) nocons


areg log_real_agwage interact2 chand_reform i.yr_qtr $indiv_controls  $dist_controls nssdist_* [aw=weight], cluster(new_state_dist_code) absorb(new_state_dist_code)
outreg2 using chand_robust.xls, append  ctitle("") keep(interact2) addtext(District FE, YES, Year Quarter FE, YES, Controls, YES, District Specific Linear Time Trend, YES) nocons
****************************************************************
use "$filepath\NSS_50\02_04_final_agri_cult_43_68_data_for_reg_02_07_all_regs_02_14.dta", clear



keep if year<2000


drop time t t1 nsstate* nsstate_*

gen time=0
replace time=2 if round==50
replace time=3 if round==55 

egen yr_qtr = concat(year sr)

destring yr_qtr, replace

tab state, gen(nsstate)
gen t2 =  time - 1
forvalues j = 1/15 {
*linear
gen nsstate_`j'=nsstate`j'*t2
}





gen ppost = (year==1994)



global indiv_controls "age hhsize hindu muslim r_others sc st c_others"



gen ptr = marketing_reform*ppost

gen ptr1 = chand_reform*ppost 

areg log_real_agwage ptr marketing_reform $indiv_controls i.yr_qtr [aw=weight], cluster(state) absorb(state)
outreg2 using placeboagwage.xls, replace  ctitle("1") keep(ptr) addtext(District FE, YES, Year FE, YES, Individual Controls, NO, State Specific Linear Time Trend, YES) nocons

areg log_real_agwage ptr1 chand_reform $indiv_controls i.yr_qtr [aw=weight], cluster(state) absorb(state)
outreg2 using placeboagwage.xls, append  ctitle("2") keep(ptr1) addtext(District FE, YES, Year FE, YES, Individual Controls, NO, State Specific Linear Time Trend, YES) nocons
********************************************
*** summary stats
use "C:\Users\karti\Documents\Data\regressions\paper_2\data_corrected_for_regs_DDD_with nssdist.dta", clear

egen tag=tag(new_state_dist_code)
tab marketing_reform if tag

gen OUTn = _n
gen OUTvar = ""

forvalues cat=1/3{
	gen Category`cat' = .
	gen OUT`cat' = .
}

replace Category1 = marketing_reform==0
replace Category2 = marketing_reform==1  

local n=0
foreach var of varlist log_real_agwage age hhsize hindu muslim r_others sc st c_others wheat_prod rice_prod no_agri_worker_united agri_worker{

 local n = `n' + 1
	quietly replace OUTvar = "`var'" if OUTn == `n'
	forvalues cat = 1/2 {
		quietly reg `var' if Category`cat' == 1, cluster(new_state_dist_code)
		local mean`cat'=_b[_cons]
		local sd`cat'=sqrt(e(rss)/e(df_r))
		local N`cat'=e(N)
		quietly replace OUT`cat' = _b[_cons] if OUTn == `n' 
	}

	local Diff=`mean1'-`mean2'
	local Welch=abs((`mean1'-`mean2')/ sqrt(`sd1'^2/`N1'+ `sd2'^2/`N2'))
	local Welch_df=(`sd1'^2/`N1'+ `sd2'^2/`N2')^2 / (`sd1'^4/`N1'^2/(`N1'-1)+ `sd2'^4/`N2'^2/(`N2'-1))
	local tprob=ttail(`Welch_df', `Welch')*2
	quietly replace OUT3 = `tprob' if OUTn == `n' 
	}
	local n = `n' + 1
replace OUTvar = "District Observations" if OUTn == `n'
foreach cat in 1 2 {
	cap drop DistTag
	egen DistTag = tag(new_state_dist_code) if Category`cat' == 1	
	quietly count if ///
	DistTag == 1 & Category`cat' == 1
	quietly replace OUT`cat' = `r(N)' if OUTn == `n' 
}


list OUTvar OUT1 OUT2 OUT3 if !missing(OUTvar)


outsheet OUTvar OUT1 OUT2 OUT3 ///
	using "C:\Users\karti\Documents\Data\regressions\paper_2\nsssumm.csv" ///
	if !missing(OUTvar), replace comma
